<!--#include file="../nav/admin_top.php" -->  
<script type="text/javascript">
var url_save = '<?=url::build('admin_combo','zh_modify',array('reqtype'=>'json'))?>'; 
var url_return = '<?=url::build('admin_combo','zh')?>';   
var nowindex = 0;
var veges = <?=json_encode($veges)?>;
var rs = <?=json_encode($rs)?>;
var rs_items = <?=json_encode($rs_items)?>;
$().ready(function(){  
	showLoading();   
  $('#name').focus();
	o2js.build('dates'); 
	o2js.build('jscalendar');
	
	Calendar.setup({
		inputField:'bdate',
		button:'slt_bdate',
		ifFormat:"%Y-%m-%d %H:%M:00",
		showsTime:true
	});
	Calendar.setup({
		inputField:'edate',
		button:'slt_edate',
		ifFormat:"%Y-%m-%d %H:%M:00",
		showsTime:true
	});
	
	Calendar.setup({
		inputField:'sendbdate',
		button:'slt_sendbdate',
		ifFormat:"%Y-%m-%d",
		showsTime:false
	});
	Calendar.setup({
		inputField:'sendedate',
		button:'slt_sendedate',
		ifFormat:"%Y-%m-%d",
		showsTime:false
	});
	
	
	fnInitPage();
});

/* 初始化页面显示 */
function fnInitPage(){
	if(rs.point_flag == '1'){
		$('#jifentbody').show();
	}else{
		$('#jifentbody').hide();
	}
	if(rs.point_type == '1'){
		$('#jifen_gd_tr').show();
		$('#jifen_order_tr').hide();
	}else{
		$('#jifen_gd_tr').hide();
		$('#jifen_order_tr').show();
	}
	
	//展示菜品
	for(var i=0; i<rs_items.length; i++){
			var obj = rs_items[i];
			obj.price = veges[obj.vege_id].price;
			var html = '';
			html += '<tr class="vegetr" id="vegetr_'+nowindex+'" i="'+nowindex+'">';
			html += ' <td id="show_index_'+nowindex+'" class="show_index"></td>';
			html += ' <td id="vege_id_'+nowindex+'" class="vege_id" val="'+obj.vege_id+'" price="'+obj.price+'" >';
			html += obj.vege_name;
			html += ' </td>';
			html += ' <td>';
			html += ' 	<img title="点击移除本行" src="'+baseUrl +'misc/images/icos/del.gif" class="sltimg" onclick="fnDelVege('+nowindex+')" />';
			html += ' </td>';
			html += '</tr>';
			$('#veges').append(html);
			nowindex++;
	}
	fnSetIndex();
}


/* 移除一项产品 */
function fnDel(id){
	fnConfirm({
		str:'确定要移除本行菜品吗？',
		func_true:function(){
			$('#protr_'+id).remove();
			fnSetIndex();
		}
	});
}
/* 设置产品项序号 */
function fnSetIndex(){
	var i = 1;
	$('td.show_index').each(function(e){
		$(this).html(i++);
	});
}

/* 添加产品 */
function fnAddPro(){
	GfnSelectVege('fnAddProBuild','选择菜品');
}
function fnAddProBuild(e){
	for(var i=0; i<e.vege_ids.length ; i++){
		var obj = e.vege_ids[i];
		var tmp_goon = true;
		//判断是否已经添加
		$('.vege_id').each(function(){
			if(obj.id ==  $(this).attr('val')){
				tmp_goon = false;
			}
		});
		if(tmp_goon){
			obj.price = veges[obj.id].price;
			var html = '';
			html += '<tr class="vegetr" id="vegetr_'+nowindex+'" i="'+nowindex+'">';
			html += ' <td id="show_index_'+nowindex+'" class="show_index"></td>';
			html += ' <td id="vege_id_'+nowindex+'" class="vege_id" val="'+obj.id+'" price="'+obj.price+'" >';
			html += obj.name;
			html += ' </td>';
			html += ' <td>';
			html += ' 	<img title="点击移除本行" src="'+baseUrl +'misc/images/icos/del.gif" class="sltimg" onclick="fnDelVege('+nowindex+')" />';
			html += ' </td>';
			html += '</tr>';
			$('#veges').append(html);
			nowindex++;
		}
	}	
	fnSetIndex();
}
function fnDelVege(id){
	$('#vegetr_'+id).remove();
	fnSetIndex();
}
/* 保存 */
function fnSave(){ 
	var form = serializeObj("#myform");
	var veges = [];
	if(form.name == ''){
		alert('请填写【组合菜品套餐】');
		$('#name').focus();
		return false;
	}
	
	if(dateDiff('s',form.bdate,form.edate) < 0){
		alert('【下单起止日期】中，结束日期必须大约开始日期');
		$('#edate').focus();
		return false;
	}
	if(dateDiff('d',form.sendbdate,form.sendedate) < 0){
		alert('【送货起止日期】中，结束日期必须大约开始日期');
		$('#sendedate').focus();
		return false;
	}
	if(dateDiff('d',form.edate.substr(0,10),form.sendedate) < 3){
		alert('【送货结束日期】必须大于【下单结束日期】至少3天或3天以上');
		$('#sendedate').focus();
		return false;
	}

	if(form.min_num == '' || !fnRegExp('reg_int1',form.min_num)){
		alert('请正确填写【最小成套数量】');
		$('#min_num').focus();
		return false;
	}
	if(form.min_price == '' || !fnRegExp('reg_num1',form.min_price)){
		alert('请正确填写【最小成套总价】');
		$('#min_price').focus();
		return false;
	}


	if(form.point_flag == '0'){
		//不赠送积分
		form.point_type = 1;
		form.point_fix = 0;
		form.point_every = 0;
	}else{
		//增送积分
		if(form.point_type == '1'){
			//固定积分模式
			if(form.point_fix == '' || !fnRegExp('reg_int1',form.point_fix)){
				alert('请正确填写【固定赠送积分数】');
				$('#point_fix').focus();
				return false;
			}
			form.point_every = 0;
		}else{
			//消费金额模式
			if(form.point_every == '' || !fnRegExp('reg_num1',form.point_every)){
				alert('请正确填写【消费金额】');
				$('#point_every').focus();
				return false;
			}
			form.point_fix = 0;
		}
	}
	
	
	//包含蔬菜
	$('#veges tr.vegetr').each(function(){
		var i = $(this).attr('i');
		veges.push({
			vege_id:$('#vege_id_'+i).attr('val')
		});
	});
	if(veges.length <= 0){
		alert('请添加可选菜品');
		return false;
	}
  $('button').attr('disabled','disabled');
	$.ajax({
		async:true,
		type:'post',
		url:url_save,
		data:{
			form:JSON.encode(form),
			veges:JSON.encode(veges)
		},
		success:function(e){
  		$('button').attr('disabled','');
			var ok = errorHandler(e);
			if(ok){　 
				jumpTo(url_return);
			}
		}
	});
}
</script>
<div class="titleDiv">
	<div class="titleCaption">
		<span class="captionSpan">菜品套餐 - 修改组合套餐</span>
	</div>
</div>
<div  class="button_div">
		<button class="btn_save" onclick="fnSave()">保 存</button>
		<span class="gray">或</span> 
		<button class=" " onclick="jumpTo(url_return);return false;">返 回</button>
</div>  
<form id="myform" action="" method="post" onsubmit="return false;">
<div class="blockdiv">
	<div class="titlediv">基本信息</div>
	<table class="mytable" > 
		<tr>
			<td class="mylabel"><span class="red">*</span>组合菜品套餐：</td>
			<td class="myinfo" > 
				<input type="hidden" id="id" name="id"  value="{$rs.id}" />
				<input type="text" class="textBox" id="name" name="name" maxlength="100" style="width:80%" value="{$rs.name}" />
			</td>  
		</tr> 
		<tr>
			<td class="mylabel">发布状态：</td>
			<td class="myinfo" > 
				<label class="green">
					<input type="radio" name="publishs" value="1"  <?php if($rs['publishs'] == '1') echo 'checked="checked"';?>  />
					发布
				</label>
				<label class="red">
					<input type="radio" name="publishs" value="0"  <?php if($rs['publishs'] == '0') echo 'checked="checked"';?>  />
					暂停
				</label>
			</td>  
		</tr>
		<tr>
			<td class="mylabel">下单起止日期：</td>
			<td class="myinfo" > 
				<input type="text" class="textBox"  style="width:160px;"  value="{$rs.bdate}"  id="bdate" name="bdate" readonly="readonly" />
				<input type="button" class="btnicon_calendar" id="slt_bdate" /> 至 
				<input type="text" class="textBox"  style="width:160px;"  value="{$rs.edate}"  id="edate" name="edate" readonly="readonly" />
				<input type="button" class="btnicon_calendar" id="slt_edate" />
			</td>  
		</tr> 
		<tr>
			<td class="mylabel">送货起止日期：</td>
			<td class="myinfo" > 
				<input type="text" class="textBox"  style="width:160px;"  value="{$rs.sendbdate}"  id="sendbdate" name="sendbdate" readonly="readonly" />
				<input type="button" class="btnicon_calendar" id="slt_sendbdate" /> 至 
				<input type="text" class="textBox"  style="width:160px;"  value="{$rs.sendedate}"  id="sendedate" name="sendedate" readonly="readonly" />
				<input type="button" class="btnicon_calendar" id="slt_sendedate" />
			</td>  
		</tr> 
		<tr>
			<td class="mylabel"><span class="red">*</span>最小成套数量：</td>
			<td class="myinfo"  > 
			<input type="text" class="textBox" id="min_num" name="min_num" value="{$rs.min_num}" maxlength="100" style="width:80px;text-align:right" /> 份
			</td>  
		</tr> 
		<tr>
			<td class="mylabel"><span class="red">*</span>最小成套总价：</td>
			<td class="myinfo"  > 
			<input type="text" class="textBox" id="min_price" name="min_price" value="{$rs.min_price}"  maxlength="10" style="width:80px;text-align:right" /> 元
			</td>  
		</tr> 
		<tr>
			<td class="mylabel">说明：</td>
			<td class="myinfo" >
				<textarea class="textBox" id="remark" name="remark" style="width:80%;height:100px;">{$rs.remark}</textarea>   
			</td>  
		</tr>
	</table> 
</div>
<div class="blockdiv">
	<div class="titlediv">积分规则</div>
	<table class="mytable" > 
		<tr>
			<td class="mylabel">是否赠送积分：</td>
			<td class="myinfo" colspan="3" > 
				<label class="green">
					<input type="radio" name="point_flag" value="1" <?php if($rs['point_flag'] == '1') echo 'checked="checked"';?>  onclick="$('#jifentbody').show()" />
					赠送
				</label>
				<label class="red">
					<input type="radio" name="point_flag" value="0" <?php if($rs['point_flag'] == '0') echo 'checked="checked"';?>  onclick="$('#jifentbody').hide()"  />
					不赠送
				</label>
			</td>  
		</tr> 
		<tbody id="jifentbody" style="display:none">
			<tr >
				<td class="mylabel">积分赠送规则</td>
				<td class="myinfo" > 
					<label class=" ">
						<input type="radio" name="point_type" value="1" <?php if($rs['point_type'] == '1') echo 'checked="checked"';?> onclick="$('#jifen_gd_tr').show();$('#jifen_order_tr').hide();" />
						固定积分模式
					</label>
					<label class=" ">
						<input type="radio" name="point_type" value="0" <?php if($rs['point_type'] == '0') echo 'checked="checked"';?> onclick="$('#jifen_gd_tr').hide();$('#jifen_order_tr').show();"  />
						订单消费金额模式
					</label>
				</td>  
			</tr>
			<tr id="jifen_gd_tr" >
				<td class="mylabel"> </td>
				<td class="myinfo" > 
				固定赠送 <input type="text" class="textBox" id="point_fix" name="point_fix" value="{$rs.point_fix}" maxlength="8" style="width:50px;text-align:right" /> 分
				</td>  
			</tr>
			<tr id="jifen_order_tr" style="display:none" >
				<td class="mylabel"> </td>
				<td class="myinfo" > 
				每消费 <input type="text" class="textBox" id="point_every" name="point_every" value="{$rs.point_every}" maxlength="8" style="width:50px;text-align:right" /> 元积 1 分
				</td>  
			</tr>
		</tbody>
	</table> 
</div>
</form>
<div class="blockdiv">
	<div class="titlediv">包含可选菜品</div>
	<div style="margin:10px auto">
		<a href="javascript:void(0)" onclick="fnAddPro()"><img src="/misc/images/icos/add2.gif" />添加菜品</a>
	</div>
	<div style="margin:5px auto">
		<table class="innserTb">
			<thead>
				<tr>
					<th style="width:60px">序号</th>
					<th style="">菜品</th>
					<th style="width:40px">移除</th>
				</tr>
			</thead>
			<tbody id="veges">
		 
			</tbody>
		</table>
	</div>
</div>	
<div  class="button_div">
	<button class="btn_save" onclick="fnSave()">保 存</button>
	<span class="gray">或</span> 
	<button class=" " onclick="jumpTo(url_return);return false;">返 回</button>
</div>  
<!--#include file="../nav/admin_bottom.php" -->